#include <gtest/gtest.h>
#include <string>
#include <msgpack.hpp>
#include "base/Bytes.h"
#include "base/Serializer.h"
#include "base/Message.h"
#include "test_Serializer.pb.h"
using namespace zeroplus::mq;

// 测试 protobuf 消息与 Bytes 之间的序列化和反序列化
TEST(ConvertTest, ProtobufBytes)
{
    using namespace zeroplus::mq::Serializer;
    using namespace test;

    test::Message srcProto;
    srcProto.set_value("Hello, Protobuf!");

    Bytes destBytes;
    EXPECT_TRUE(convert(srcProto, destBytes));

    test::Message recoveredProto;
    EXPECT_TRUE(convert(destBytes, recoveredProto));
    EXPECT_EQ(recoveredProto.value(), srcProto.value());
}

// 测试 复杂msgpack 结构体与 Bytes 之间的序列化和反序列化
TEST(ConvertTest, MsgpackBytes)
{
    using namespace zeroplus::mq::Serializer;

    test::Message data;
    data.set_value("Hello, Protobuf!");
    Message<test::Message> srcMsgpack = Message("lcf", "Message", data, 1);

    Bytes destBytes;
    EXPECT_TRUE(convert(srcMsgpack, destBytes));

    Message<test::Message> recoveredMsgpack;
    EXPECT_TRUE(convert(destBytes, recoveredMsgpack));
    // EXPECT_EQ(recoveredMsgpack.getBody(), srcMsgpack.getBody());
    LOG_INFO("%s", recoveredMsgpack.getBody().value().c_str());
}

// 测试 string 与 Bytes 之间的序列化和反序列化
TEST(ConvertTest, Convert)
{
    using namespace zeroplus::mq::Serializer;

    std::string srcString = "Hello, String!";

    Bytes destBytes;
    EXPECT_TRUE(convert(srcString, destBytes));

    std::string recoveredString;
    EXPECT_TRUE(convert(destBytes, recoveredString));
    EXPECT_EQ(recoveredString, srcString);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}